package com.ss.android.article.common;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.a.a.NightMode;
import com.bytedance.common.utility.UIUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.e.TTAndroidObject;
import com.ss.android.article.base.ui.LoadingFlashView;
import com.ss.android.common.util.LoadUrlUtils;
import com.ss.android.newmedia.a.BrowserFragment;
import com.ss.android.newmedia.a.BrowserFragment.WebViewClientListener;
import com.ss.android.newmedia.webview.SSWebView;

import java.util.List;

public abstract class BaseBrowserFragment extends BrowserFragment implements NightMode, WebViewClientListener {
    private LoadingFlashView mAnimView;
    private boolean mShowLoadAnim = true;
    private boolean mIsFirstRefresh = true;
    private PullToRefreshSSWebView mPullWebView;
    private View mCoverView;
    private boolean mEnablePullRefresh = true;
    private boolean mUserCover = false;
    private FrameLayout mAnimLayout;
    public String mBaseUrl;

    public final SSWebView getWebView(View view) {
        this.mPullWebView = view.findViewById(com.ss.android.article.browser.R.id.webview);
        this.mPullWebView.setScrollingWhileRefreshingEnabled(true);
        SSWebView ssWebView = this.mPullWebView.getRefreshableView();
        ssWebView.getSettings().setJavaScriptEnabled(true);
        ssWebView.getSettings().setDomStorageEnabled(true);
        return ssWebView;
    }

    public void initTTAndroidObject() {
        if (this.mJsObject == null) {
            TTAndroidObject ttAndroidObject = new TTAndroidObject(AppData.getInstance(), this.getActivity());
            ttAndroidObject.setLargeImageContext(this);
            ttAndroidObject.setWebView(this.mWebview);
            ttAndroidObject.setFragment(this);
            this.mJsObject = ttAndroidObject;
        }
    }

    public void onPageFinished() {
        this.mPullWebView.onRefreshComplete();
        AppData.getInstance();
        String js = AppData.isNightMode() ? "javascript: TouTiao.setDayMode(0)" : "javascript: TouTiao.setDayMode(1)";
        LoadUrlUtils.loadUrl(this.mWebview, js);
        if (this.mShowLoadAnim) {
            UIUtils.setVisibility(this.mAnimLayout, View.GONE);
            UIUtils.setVisibility(this.mAnimView, View.GONE);
            this.mAnimView.stop();
            this.mShowLoadAnim = false;
        }
    }

    public abstract void onPullToRefresh();

    public final int getLayoutId() {
        return com.ss.android.article.browser.R.layout.base_browser_fragment;
    }

    public final void tryRefreshTheme() {
        super.tryRefreshTheme();
        if (this.mPullWebView != null) {
            com.ss.android.j.b.isNightModeToggled();
            this.mPullWebView.getLoadingLayoutProxy().setTheme(false);
            LoadUrlUtils.loadUrl(this.mWebview, "javascript: TouTiao.setDayMode(1)");
        }
    }

    final void pullToRefresh() {
        this.onPullToRefresh();
        System.currentTimeMillis();
    }

    public final void onPageStarted() {
        if (this.mIsFirstRefresh) {
            this.mPullWebView.setRefreshing(true);
            this.mIsFirstRefresh = false;
        }

        if (this.mShowLoadAnim) {
            List v1 = this.mPullWebView.getHeaderLayoutList();
            if (v1 != null && v1.size() > 0) {
                ViewGroup.LayoutParams v0 = this.mAnimLayout.getLayoutParams();
                ((ViewGroup.MarginLayoutParams) v0).setMargins(((ViewGroup.MarginLayoutParams) v0).leftMargin, ((com.handmark.pulltorefresh.library.internal.LoadingLayout) v1.get(0)).getContentSize(), ((ViewGroup.MarginLayoutParams) v0).rightMargin, ((ViewGroup.MarginLayoutParams) v0).bottomMargin);
            }

            UIUtils.setVisibility(this.mAnimLayout, 0);
            UIUtils.setVisibility(this.mAnimView, 0);
            this.mAnimView.start();
        }
    }

    public final void onPageReceivedError() {
        this.mPullWebView.onRefreshComplete();
    }

    public void onActivityCreated(Bundle bundle) {
        this.setOnPageLoadListener(this);
        if (this.getArguments() != null) {
            this.getArguments().getString("key");
            this.mBaseUrl = this.getArguments().getString("url");
            this.getArguments().getBoolean("enable_refresh");
            this.getArguments().getInt("refresh_min_interval");
            this.mEnablePullRefresh = this.getArguments().getBoolean("enable_pull_refresh", true);
            this.mUserCover = this.getArguments().getBoolean("user_cover", false);
        }

        super.onActivityCreated(bundle);
        if (!this.mUserCover) {
            this.mCoverView.setVisibility(View.GONE);
        }

        if (this.mEnablePullRefresh) {
            this.mPullWebView.setMode(Mode.PULL_FROM_START);
            this.mPullWebView.setOnRefreshListener(new com.ss.android.article.common.bb(this));
        } else {
            this.mPullWebView.setMode(Mode.DISABLED);
        }
        this.pullToRefresh();
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(layoutInflater, container, savedInstanceState);
        this.mCoverView = view.findViewById(com.ss.android.article.browser.R.id.browser_cover);
        this.mAnimLayout = view.findViewById(com.ss.android.article.browser.R.id.load_anim_frame);
        this.mAnimView = view.findViewById(com.ss.android.article.browser.R.id.load_anim_view);
        return view;
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onDestroyView() {
        super.onDestroyView();
    }
}

